<template>
  <div class="match-detail-container">
    <div class="page-header">
      <el-page-header content="匹配详情" @back="goBack"/>
    </div>

    <el-card v-loading="loading" class="match-card">
      <template #header>
        <div class="card-header">
          <h3>三单匹配详情</h3>
          <div class="match-status">
            <el-tag v-if="matchInfo.matchResult" :type="matchInfo.matchResult === '匹配' ? 'success' : 'danger'">
              {{ matchInfo.matchResult }}
            </el-tag>
          </div>
        </div>
      </template>

      <div class="document-section">
        <h4>订单信息</h4>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="订单编号">{{ matchInfo.orderNumber }}</el-descriptions-item>
          <el-descriptions-item label="供应商">{{ matchInfo.supplierName }}</el-descriptions-item>
          <el-descriptions-item label="订单金额">{{ formatAmount(matchInfo.orderAmount) }}</el-descriptions-item>
          <el-descriptions-item label="订单状态">{{ matchInfo.orderStatus }}</el-descriptions-item>
          <el-descriptions-item label="采购日期">{{ matchInfo.purchaseDate }}</el-descriptions-item>
        </el-descriptions>
      </div>

      <div class="document-section">
        <h4>收货单信息</h4>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="收货单编号">{{ matchInfo.receiptNumber }}</el-descriptions-item>
          <el-descriptions-item label="收货单金额">{{ formatAmount(matchInfo.receiptAmount) }}</el-descriptions-item>
          <el-descriptions-item label="收货单状态">{{ matchInfo.receiptStatus }}</el-descriptions-item>
        </el-descriptions>
      </div>

      <div class="document-section">
        <h4>发票信息</h4>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="发票编号">{{ matchInfo.invoiceNumber }}</el-descriptions-item>
          <el-descriptions-item label="发票金额">{{ formatAmount(matchInfo.invoiceAmount) }}</el-descriptions-item>
          <el-descriptions-item label="开票日期">{{ matchInfo.issueDate }}</el-descriptions-item>
        </el-descriptions>
      </div>

      <div v-if="matchInfo.difference" class="document-section difference-section">
        <h4>差异信息</h4>
        <div class="difference-content">
          {{ matchInfo.difference }}
        </div>
      </div>

      <div class="document-section">
        <h4>匹配时间</h4>
        <div class="match-time">{{ formatDateTime(matchInfo.matchTime) }}</div>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import {onMounted, ref} from 'vue';
import {useRoute, useRouter} from 'vue-router';
import {ElMessage} from 'element-plus';
import {getMatchDetail} from '@/api/match';
import type {MatchedOrderVO} from '@/api/types';

const route = useRoute();
const router = useRouter();
const matchId = route.params.id as string;
const loading = ref(true);
const matchInfo = ref<Partial<MatchedOrderVO>>({});

// 获取匹配详情
const fetchMatchDetail = async () => {
  try {
    loading.value = true;
    const res = await getMatchDetail(matchId);
    if (res.code === 200) {
      matchInfo.value = res.data;
    } else {
      ElMessage.error(res.message || '获取匹配详情失败');
    }
  } catch (error) {
    console.error('获取匹配详情失败', error);
    ElMessage.error('获取匹配详情失败');
  } finally {
    loading.value = false;
  }
};

// 返回上一页
const goBack = () => {
  router.back();
};

// 格式化金额
const formatAmount = (amount: number | undefined) => {
  if (amount === undefined) return '--';
  return `¥ ${amount.toFixed(2)}`;
};

// 格式化日期时间
const formatDateTime = (dateTime: string | undefined) => {
  if (!dateTime) return '--';
  return dateTime.replace('T', ' ').substring(0, 19);
};

onMounted(() => {
  fetchMatchDetail();
});
</script>

<style scoped>
.match-detail-container {
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
}

.match-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.document-section {
  margin-bottom: 30px;
}

.document-section h4 {
  margin-bottom: 15px;
  padding-bottom: 8px;
  border-bottom: 1px solid #ebeef5;
  color: #303133;
  font-weight: 500;
}

.difference-section {
  margin-top: 20px;
}

.difference-content {
  background-color: #fef0f0;
  padding: 15px;
  border-radius: 4px;
  color: #f56c6c;
  font-size: 14px;
  line-height: 1.5;
}

.match-time {
  color: #606266;
  font-size: 14px;
}
</style> 